package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import service.MemberService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * ClassName: MemberServiceImpl
 * Description:
 * date: 2019/11/25 19:31
 * Author Airson
 * Since JDK 1.8
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    /**
     * 注册用户
     */
    public void handlermember(Map<String, String> map) {
        //判断是否是会员
        String telephone = map.get("telephone");
        //查询表格t_member
        Member member = memberDao.findMemberByTelephoneNumber(telephone);

        if (member == null) {
            //没有该会员,注册
            //创建会员
            Member newMember = new Member();
            //设置会员电话号
            newMember.setPhoneNumber(telephone);
            //设置会员注册时间
            newMember.setRegTime(new Date());
            //设置会员编号=注册时间码加4位随机数
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String fileNumber = sdf.format(new Date()) + ValidateCodeUtils.generateValidateCode(4);
            newMember.setFileNumber(fileNumber);

            //将会员添加到t_member中
            memberDao.add(newMember);
            System.out.println("注册成功-----");
        }
    }
}
